package com.taiji.dianthus.controller.sms;

import com.taiji.dianthus.common.BasicController;
import com.taiji.dianthus.common.BusinessMessage;
import com.taiji.dianthus.domain.sms.SmsInfo;
import com.taiji.dianthus.domain.sys.DSysUser;
import com.taiji.dianthus.service.sms.SmsInfoService;
import com.taiji.dianthus.service.sys.OrgService;
import com.taiji.dianthus.service.sys.SysLogService;
import com.taiji.dianthus.sms.Sdk2SMSSendService;
import com.taiji.dianthus.util.IPUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import static com.taiji.dianthus.common.Constant.*;

/**
 * @ClassName SmsDraftsViewController
 * @Description 已删除短信
 * @Author H.M
 * @Date 2020/7/15
 */
@RequestMapping(value = "/sms/del")
@Controller
public class SmsDelViewController extends BasicController {

    @Autowired
    private SysLogService sysLogService;
    @Autowired
    private OrgService orgService;
    @Autowired
    private SmsInfoService smsInfoService;



    /**
     * 页面
     *
     * @param model
     * @return
     */
    @RequestMapping(value = "/list")
    public String sendMessageIndex(HttpServletRequest request, Model model, Integer pageSize, Integer currentPage, String startDate, String endDate) {
        try {
            currentPage = getCurrentPage(currentPage);
            pageSize = getPageSize(pageSize);
            DSysUser currentUser = currentUser();
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");//注意月份是MM
            String url = "/sms/del/list?pageSize=10";
            Date start = null;
            if (!StringUtils.isEmpty(startDate)) {
                url += "&startDate=" + startDate;
                model.addAttribute("startDate", startDate);
                Calendar calendar = Calendar.getInstance();
                calendar.setTime(simpleDateFormat.parse(startDate));
                calendar.set(Calendar.HOUR_OF_DAY, 0);
                calendar.set(Calendar.MINUTE, 0);
                calendar.set(Calendar.SECOND, 0);
                start = calendar.getTime();
            }
            Date end = null;
            if (!StringUtils.isEmpty(endDate)) {
                url += "&endDate=" + endDate;
                model.addAttribute("endDate", endDate);
                Calendar calendar = Calendar.getInstance();
                calendar.setTime(simpleDateFormat.parse(endDate));
                calendar.set(Calendar.HOUR_OF_DAY, 23);
                calendar.set(Calendar.MINUTE, 59);
                calendar.set(Calendar.SECOND, 59);
                end = calendar.getTime();
            }
            if (currentPage != null) {
                model.addAttribute("currentPage", currentPage + 1);
            }
            BusinessMessage message = smsInfoService.draftsList(currentPage, pageSize, start, end, currentUser.getId(), 3);
            model.addAttribute("url", url);
            model.addAttribute("data", message.getData());
            model.addAttribute("currentUser", currentUser);
            model.addAttribute("menus", listMenu());
            sysLogService.save(currentUser(), LOG_OPERATION_QUERY,
                    IPUtil.getIpAddr(request), "查询已删除列表", message.success ? 1 : 0, message.getMsg());
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return "/pages/sms/smsDelList";
    }

    @RequestMapping("/delete")
    @ResponseBody
    public BusinessMessage delete(HttpServletRequest request, String id) {
        BusinessMessage message = smsInfoService.deleteDrafts(id);
        sysLogService.save(currentUser(), LOG_OPERATION_DEL,
                IPUtil.getIpAddr(request), "删除已删除记录", message.success ? 1 : 0, message.getMsg());
        return message;
    }

    @RequestMapping("/reduction")
    @ResponseBody
    public BusinessMessage reduction(HttpServletRequest request, String id) {
        BusinessMessage message = smsInfoService.updateMsgLoc(id, 1);
        if (message.getSuccess()) {
            message.setMsg("还原已删除记录成功");
        } else {
            message.setMsg("还原失败，系统错误");
        }
        sysLogService.save(currentUser(), LOG_OPERATION_SAVE,
                IPUtil.getIpAddr(request), "还原已删除记录", message.success ? 1 : 0, message.getMsg());
        return message;
    }

    /**
     * 草稿箱详情
     *
     * @param model
     * @return
     */
    @RequestMapping(value = "/one")
    public String getSmsOne(HttpServletRequest request, Model model, Integer currentPage, String startDate, String endDate, String id) {
        try {
            DSysUser currentUser = currentUser();
            String url = "/sms/del/list?pageSize=10";
            if (!StringUtils.isEmpty(startDate)) {
                url += "&startDate=" + startDate;
            }
            if (!StringUtils.isEmpty(endDate)) {
                url += "&endDate=" + endDate;
            }
            if (currentPage != null) {
                url += "&currentPage=" + currentPage;
            }
            SmsInfo info = smsInfoService.getOne(id);
            model.addAttribute("url", url);
            model.addAttribute("data", info);
            model.addAttribute("currentUser", currentUser);
            model.addAttribute("menus", listMenu());
            sysLogService.save(currentUser(), LOG_OPERATION_QUERY,
                    IPUtil.getIpAddr(request), "跳转已删除详情页面", 1, "查看已删除详情");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "/pages/sms/smsDelOne";
    }


}
